/**
 * Copyright 2011 Adrian Witas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.abstractmeta.commandify.core.command.collection;

import org.abstractmeta.commandify.core.command.AbstractCommand;
import org.abstractmeta.commandify.core.command.AbstractMethodCommand;

import java.util.Iterator;
import java.util.NavigableSet;

/**
 * Represents a sortedSet which implement commandify.
 * This enables all mutable collection method calls being encapsulated as method commands.
 * If collection value implements commandify then as long as it is possible to build cloned version build, it will be used
 * instead of actual value.
 */
public class CommandifyNavigableSet<T> extends CommandifySortedSet<T> implements NavigableSet<T> {

    protected final NavigableSet<T> navigableSet;

    public CommandifyNavigableSet(NavigableSet<T> navigableSet) {
        super(NavigableSet.class, navigableSet);
        this.navigableSet = navigableSet;
    }

    @Override
    public T lower(T t) {
        return navigableSet.lower(t);
    }

    @Override
    public T floor(T t) {
        return navigableSet.floor(t);
    }

    @Override
    public T ceiling(T t) {
        return navigableSet.ceiling(t);
    }

    @Override
    public T higher(T t) {
        return navigableSet.higher(t);
    }

    @Override
    @SuppressWarnings("unchecked")
    public T pollFirst() {
        AbstractCommand command = new AbstractMethodCommand<NavigableSet<T>>("pollFirst") {
            @Override
            public void execute(NavigableSet<T> instance) {
                instance.pollFirst();
            }

        };
        getCommands().add(command);
        return navigableSet.pollFirst();
    }

    @Override
    @SuppressWarnings("unchecked")
    public T pollLast() {
        AbstractCommand command = new AbstractMethodCommand<NavigableSet<T>>("pollLast") {
            @Override
            public void execute(NavigableSet<T> instance) {
                instance.pollLast();
            }

        };
        getCommands().add(command);
        return navigableSet.pollLast();
    }

    @Override
    public NavigableSet<T> descendingSet() {
        return navigableSet.descendingSet();
    }

    @Override
    public Iterator<T> descendingIterator() {
        return navigableSet.descendingIterator();
    }

    @Override
    public NavigableSet<T> subSet(T t, boolean b, T t2, boolean b2) {
        return navigableSet.subSet(t, b, t2, b2);
    }

    @Override
    public NavigableSet<T> headSet(T t, boolean b) {
        return navigableSet.headSet(t, b);
    }

    @Override
    public NavigableSet<T> tailSet(T t, boolean b) {
        return navigableSet.tailSet(t, b);
    }
}
